package com.tzxs.pay.main.mine;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivitySettingBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.login.LoginActivity;
import com.tzxs.pay.main.service.UpdateService;
import com.tzxs.pay.util.ISharedPreference;
import com.tzxs.pay.util.StatusBarColorSetter;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity<ActivitySettingBinding> implements View.OnClickListener {
    private RxPermissions rxPermissions;
    private static final String TAG = SettingActivity.class.getSimpleName();
    private ISharedPreference sharedPreference;

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#FAFAFA"));
        rxPermissions = new RxPermissions(this);
        dataBinding.changePayPassword.setOnClickListener(this);
        dataBinding.loginOut.setOnClickListener(this);
        if (ISharedPreference.getInstance(getApplication()).getReceiveNotice()) {
            dataBinding.notice.setChecked(true);
        } else {
            dataBinding.notice.setChecked(false);
        }
        dataBinding.settingToolbar.setNavigationOnClickListener(v -> onBackPressed());
        sharedPreference = ISharedPreference.getInstance(getApplication());
        //设置接收通知
        dataBinding.notice.setChecked(sharedPreference.getReceiveNotice());
        try {
            dataBinding.version.setText(String.format("当前版本:%s", getPackageManager().getPackageInfo(getPackageName(), 0).versionName));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.change_pay_password:
                intent = new Intent(this, PasswordChangeActivity.class);
                break;
            case R.id.update_info:
                //在这里做app更新
//                intent = new Intent(this, UpdateAppActivity.class);
                break;
            //停止通知
            case R.id.notice:
                if (sharedPreference.getReceiveNotice()) {
                    PushManager.getInstance().turnOffPush(this);
                    sharedPreference.saveReceiveNotice(false);
                } else {
                    PushManager.getInstance().turnOnPush(this);
                    sharedPreference.saveReceiveNotice(true);
                }
                break;
            //退出登录
            case R.id.login_out:
                //清除本地数据
                ISharedPreference.getInstance(getApplication()).loginOut();
                intent = new Intent(this, LoginActivity.class);
                //发送登录广播
                Intent cast = new Intent();
                cast.setAction(LoginOutReceiver.loginOur);
                sendBroadcast(cast);
                finish();
                break;
        }
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
      /*  if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }*/
    }

    /**
     * 更新app
     */
    @SuppressLint("CheckResult")
    private void update() {
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(permission -> {
                    if (permission.granted) {
                        //当权限存在的时候再下载
                        new Thread(() -> {
                            Intent intent = new Intent(SettingActivity.this, UpdateService.class);
                            startService(intent);
                        }).start();

                    } else if (permission.shouldShowRequestPermissionRationale) {

                        Toast.makeText(SettingActivity.this, "要下载文件,请同意文件读写权限!", Toast.LENGTH_SHORT).show();
                    } else {
                        // Denied permission with ask never again
                        // Need to go to the settings
                        Uri packageURI = Uri.parse("package:" + getPackageName());
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                        startActivity(intent);
                    }
                });
    }
}
